const passwordHash = require('yi-password-hash');
module .exports={
    open:false,
    description:'管理员修改密码',
    handler(req, res, next) {
        let doc = req.body;
        let id = doc._id;
        let oldPWD = doc.oldPWD;
        let newPWD = doc.newPWD;
        let comfirmedPWD = doc.comfirmedPWD;
        if(newPWD!=comfirmedPWD) return res.reply({code:2,msg:"新密码与确认密码输入不一致,密码修改失败!",result:null})

        req.mongo.collection("manager").find({"_id":id}).next((e,r)=>{
          if(e) return res.err.mongoError(e);
          passwordHash.compare(oldPWD,r.pwd,(e,r)=>{
            if(e) return res.err.unknownError(e);
            if(!r) return res.reply({code:1,msg:"旧密码错误,密码修改失败!",result:null})

            passwordHash.hash(newPWD,(e,r)=>{
              if(e) return res.err.unknownError(e);
              req.mongo.collection("manager").updateOne({"_id":id},{$set:{"pwd":r}},(e,r)=>{
                if(e) return res.err.mongoError(e);
                return res.reply({modifiedCount: r.modifiedCount,_id:id});
              })
            })
          })
        })
    }
};